@echo off
setlocal

REM 检测系统架构
if "%PROCESSOR_ARCHITECTURE%" == "x86" (
    if not defined PROCESSOR_ARCHITEW6432 (
        set "FRAMEWORK_PATH=C:\Windows\Microsoft.NET\Framework"
    ) else (
        set "FRAMEWORK_PATH=C:\Windows\Microsoft.NET\Framework64"
    )
) else (
    set "FRAMEWORK_PATH=C:\Windows\Microsoft.NET\Framework64"
)

if "%1"=="/i" goto install
if "%1"=="/u" goto uninstall
if "%1"=="/r" goto reinstall
goto usage

:install
echo 创建服务...
sc create WebDavSyncService binPath= "%~dp0WebDavSyncTool.exe" start= auto DisplayName= "WebDAV Sync Service"
echo 设置服务描述...
sc description WebDavSyncService "同步本地目录到WebDAV服务器"
echo 启动服务...
sc start WebDavSyncService
goto check

:uninstall
echo 停止服务...
net stop WebDavSyncService
echo 删除服务...
sc delete WebDavSyncService
goto end

:reinstall
echo 停止服务...
net stop WebDavSyncService
echo 卸载服务...
%FRAMEWORK_PATH%\v4.0.30319\installutil.exe /u WebDavSyncTool.exe
echo 删除服务...
sc delete WebDavSyncService
echo 等待系统清理...
timeout /t 5
echo 安装服务...
%FRAMEWORK_PATH%\v4.0.30319\installutil.exe WebDavSyncTool.exe
echo 启动服务...
net start WebDavSyncService
goto check

:check
echo 检查服务状态...
sc query WebDavSyncService
goto end

:usage
echo 用法:
echo install.bat [/i^|/u^|/r]
echo   /i  - 安装服务
echo   /u  - 卸载服务
echo   /r  - 重新安装服务

:end
pause